package cn.zyq.ykb.service.tk.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.tk.MenuCollect;
import cn.zyq.ykb.beans.tk.MenuFood;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.mapper.tk.MenuCollectMapper;
import cn.zyq.ykb.mapper.tk.MenuFoodMapper;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.mapper.tk.MenuMapper;
import cn.zyq.ykb.beans.tk.Menu;
import cn.zyq.ykb.service.tk.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;
import org.springframework.util.CollectionUtils;


import java.time.LocalDateTime;
import java.util.List;

/**
 * 菜单Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-10-20
 */
@Service
public class MenuServiceImpl  extends BaseService implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuFoodMapper menuFoodMapper;
    @Autowired
    private MenuCollectMapper collectMapper;

    /**
     * 查询菜单
     *
     * @param menuID 菜单主键
     * @param tUserID
     * @return 菜单
     */
    @Override
    public Menu selectMenuByMenuID(Integer menuID, Integer tUserID) {
        Menu menu = menuMapper.selectByPrimaryKey(menuID);
        if (menu.getDeleted() == 1) {
            return null;
        }
        MenuFood pa = new MenuFood();
        pa.setMenuID(menu.getMenuID());
        pa.setDeleted(0);
        List<MenuFood> select = this.menuFoodMapper.select(pa);
        menu.setMenuFood(select);
        MenuCollect collect = new MenuCollect();
        collect.setMenuID(menuID);
        collect.setTUserID(tUserID);
        collect.setState(1);
        MenuCollect menuCollect = this.collectMapper.selectOne(collect);
        if (BeanUtil.isNotEmpty(menuCollect)){
            menu.setCollect(true);
        }
        menu.setMenuCollect(menuCollect);
        return menu;
    }

    /**
     * 查询菜单
     *
     * @param serviceID 业务ID
     * @return 菜单
     */
    @Override
    public Menu selectMenuByServiceID(String serviceID) {
        Menu param = new Menu();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.menuMapper.selectOne(param);
    }

    /**
     * 查询菜单列表
     *
     * @param pageParam
     * @return 菜单
     */
    @Override
    public PageInfo<Menu> selectMenuList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        Integer tUserID = (Integer) pageParam.getKv().get("tUserID");
        List<Menu> list = this.menuMapper.list(pageParam.getKv());
        for (Menu menu : list) {
            MenuCollect collect = new MenuCollect();
            collect.setMenuID(menu.getMenuID());
            collect.setTUserID(tUserID);
            collect.setState(1);
            MenuCollect menuCollect = this.collectMapper.selectOne(collect);
            if (BeanUtil.isNotEmpty(menuCollect)){
                menu.setCollect(true);
            }
        }
        return new PageInfo<>(list);
    }

    /**
     * 查询菜单列表
     *
     * @param pageParam
     * @return 菜单
     */
    @Override
    public List<Menu> selectMenuAllList(PageParam pageParam) {
        pageParam.getKv().put("deleted", 0);
        return this.menuMapper.list(pageParam.getKv());
    }
    /**
     * 新增菜单
     *
     * @param param 菜单
     * @return 结果
     */
    @Override
    public int insertMenu(Menu param) {
        List<MenuFood> menuFood = param.getMenuFood();
        if (CollectionUtils.isEmpty(menuFood)){
            throw new BusinessException("菜品食材不能为空");
        }
        this.setCreateBaseData(param, new User());
        int i = this.menuMapper.insertSelective(param);
        if (i == 0){
            throw new BusinessException("新增菜品失败");
        }
        for (MenuFood food : menuFood) {
            food.setMenuID(param.getMenuID());
            this.setCreateBaseData(food, new User());
            int i1 = this.menuFoodMapper.insertSelective(food);
            if (i1 == 0){
                throw new BusinessException("新增食材失败");
            }
        }
        return 1;
    }

    /**
     * 修改菜单
     *
     * @param param 菜单
     * @return 结果
     */
    @Override
    public int updateMenu(Menu param) {
        this.setUpdateBaseData(param, new User());
        return this.menuMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除菜单
     *
     * @param menuIDs 需要删除的菜单主键
     * @return 结果
     */
    @Override
    public int deleteMenuByMenuIDs(Integer[] menuIDs) {
        int i = 0;
        for (Integer menuID : menuIDs) {
            i += this.deleteMenuByMenuID(menuID);
        }
        return i;
    }

    /**
     * 删除菜单信息
     *
     * @param menuID 菜单主键
     * @return 结果
     */
    @Override
    public int deleteMenuByMenuID(Integer menuID) {
        Menu menu = this.menuMapper.selectByPrimaryKey(menuID);
        if (menu == null) {
            return 0;
        } else {
            Menu storeInfo = new Menu();
            storeInfo.setMenuID(menuID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.menuMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
